package com.itheima.service.impl;

import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    //注入持久层对象
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void importOrderSettings(List<OrderSetting> orderSettingList) {

        //调用持久层对象添加数据
        for (OrderSetting orderSetting : orderSettingList) {
            //如果用户重复上传文件 应比较相同的日期,并覆盖相同日期的预约数据
            //获得日期值
            Date orderDate = orderSetting.getOrderDate();

            //查询是否当前日期已存在预约数
            int count = orderSettingMapper.findOrderSettingByOrderDate(orderDate);

            if (count>0){
                //如果存在则更新预约数值
                orderSettingMapper.updateOrderSettingByOrderDate(orderDate,orderSetting.getNumber());

            }else {
                //不存在则添加该日期的预约数值
                orderSettingMapper.addOrderSetting(orderSetting);
            }
        }
    }

    @Override
    public List<Map> findOrderSetting(Integer currentYear, Integer currentMonth) {
        //拼接日期参数值
        String startDate=currentYear+"-"+currentMonth+"-1";
        String endDate=currentYear+"-"+currentMonth+"-31";

        //查询数据库数据
        List<Map> list = orderSettingMapper.findOrderSettingByStartDateAndEndDate(startDate,endDate);

        return list;
    }

    @Override
    public void updateOrderSetting(Integer year, Integer month, Integer day, Integer number) {
        //拼接日期
        String orderDate=year+"-"+month+"-"+day;

        System.out.println(orderDate);

        //调用持久层修改数据
        orderSettingMapper.updateOrderSetting(orderDate,number);
    }
}
